﻿// ===============================================================================
// XCI.WinForm.Platform 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System.Collections;
using System.Collections.Generic;

namespace XCI.Core
{
    /// <summary>
    /// 分页信息
    /// </summary>
    public class PageInfo
    {
        private int _recordEndIndex;
        private int _recordStartIndex;

        /// <summary>
        /// 构造分页列表
        /// </summary>
        /// <param name="pageIndex">页码索引,从1开始</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="totalCount">总记录数</param>
        public PageInfo(int pageIndex, int pageSize, int totalCount)
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = totalCount;

            if (PageSize == 0)
            {
                PageSize = 1;
            }
            TotalPages = (TotalCount + PageSize - 1) / PageSize;
        }

        /// <summary>
        /// 设置页码索引,从1开始
        /// </summary>
        /// <param name="pageIndex">页码索引,从1开始</param>
        /// <returns>返回当前对象</returns>
        public PageInfo SetPageIndex(int pageIndex)
        {
            PageIndex = pageIndex;
            return this;
        }

        /// <summary>
        /// 页码索引
        /// </summary>
        public int PageIndex { get; private set; }

        /// <summary>
        /// 每页大小
        /// </summary>
        public int PageSize { get; private set; }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalCount { get; }

        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPages { get; private set; }

        /// <summary>
        /// 当前记录开始数
        /// </summary>
        public int RecordStartIndex
        {
            get
            {
                if (TotalCount > 0)
                {
                    _recordStartIndex = (PageIndex - 1) * PageSize + 1;
                }
                return _recordStartIndex;
            }
        }

        /// <summary>
        /// 当前记录结束数
        /// </summary>
        public int RecordEndIndex
        {
            get
            {
                var start = RecordStartIndex;
                _recordEndIndex = start + PageSize - 1;
                if (_recordEndIndex > TotalCount)
                {
                    _recordEndIndex = TotalCount;
                }
                return _recordEndIndex;
            }
        }

        /// <summary>
        /// 是否首页
        /// </summary>
        public bool IsFirst => (PageIndex == 1);

        /// <summary>
        /// 是否有上一页
        /// </summary>
        public bool HasPreviousPage => (PageIndex > 1);

        /// <summary>
        /// 是否有下一页
        /// </summary>
        public bool HasNextPage => (PageIndex < TotalPages);

        /// <summary>
        /// 是否最后一页
        /// </summary>
        public bool IsLast => (PageIndex == TotalPages);

        /// <summary>
        /// 是否有数据
        /// </summary>
        public bool HasData => TotalPages > 0;
    }

    /// <summary>
    /// 分页列表
    /// </summary>
    public class PageList
    {
        /// <summary>
        /// 总记录数
        /// </summary>
        public int Total { get; set; }

        /// <summary>
        /// 页码索引,从1开始
        /// </summary>
        public int PageIndex { get; set; } = 1;

        /// <summary>
        /// 每页记录数
        /// </summary>
        public int PageSize { get; set; } = 10;

        /// <summary>
        /// 数据集合
        /// </summary>
        public IList Rows { get; set; }
    }

    /// <summary>
    /// 分页列表
    /// </summary>
    /// <typeparam name="T">元素类型</typeparam>
    public class PageList<T> : PageList
    {
        /// <summary>
        /// 数据集合
        /// </summary>
        public new List<T> Rows { get; set; }
    }
}